package net.microfigure.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.model.AreaInfo;
import net.microfigure.service.IAreaInfoService;
import net.microfigure.commons.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-15
 */
@Controller
@RequestMapping("/areaInfo")
public class AreaInfoController extends BaseController {
	@Autowired
	private IAreaInfoService areaInfoService;

	@GetMapping("/manager")
	public String manager() {
		System.out.println("admin/areaInfo/areaInfoList");
		return "admin/areaInfo/areaInfoList";
	}

	
	 /**
     * 行政区域资源树
     *
     * @return
     */
    @PostMapping(value = "/tree")
    @ResponseBody
    public Object tree() {
    	return areaInfoService.selectTree();
    }
	 /**
     * 行政区域资源树code
     *
     * @return
     */
    @PostMapping(value = "/treecode")
    @ResponseBody
    public Object treecode() {
    	return areaInfoService.selectTreecode();
    }
    
    
    /**
     * 行政区域列表
     *
     * @return
     */
    @RequestMapping("/treeGrid")
    @ResponseBody
    public Object treeGrid() {
    	return areaInfoService.selectTreeGrid();
    }
    

    
	/**
	 * 添加页面
	 * @return
	 */
	@GetMapping("/addPage")
	public String addPage() {
		return "admin/areaInfo/areaInfoAdd";
	}
    
	/**
	 * 添加
	 * @param 
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public Object add(@Valid AreaInfo areaInfo) {
		areaInfo.setCreateTime(new Date());
		
		
		boolean b = areaInfoService.insert(areaInfo);
		if (b) {
			return renderSuccess("添加成功！");
		} else {
			return renderError("添加失败！");
		}
	}

    
	/**
	 * 编辑
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/editPage")
	public String editPage(Model model, Long id) {
		AreaInfo areaInfo = areaInfoService.selectById(id);
		model.addAttribute("areaInfo", areaInfo);
		return "admin/areaInfo/areaInfoEdit";
	} 
    
    
    

	/**
	 * 编辑
	 * @param 
	 * @return
	 */
	@PostMapping("/edit")
	@ResponseBody
	public Object edit(@Valid AreaInfo areaInfo) {
		
		boolean b = areaInfoService.updateById(areaInfo);
		if (b) {
			return renderSuccess("编辑成功！");
		} else {
			return renderError("编辑失败！");
		}
	}




	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		System.out.println("打印删除id"+id);
		boolean b = areaInfoService.deleteById(id);
		if (b) {
			return renderSuccess("删除成功！");
		} else {
			return renderError("删除失败！");
		}
	}



}
